package com.salesmonitoring.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import com.salesmonitoring.connector.Connector;

public class LoginDaoImpl {

	private Connector connector;
	private Connection con;
	private Statement statement;
	private ResultSet rs;
	private String query = "";
	
	private static LoginDaoImpl loginDaoImpl;
	
	public static synchronized LoginDaoImpl getInstance() {
		
		if(loginDaoImpl == null) {
			loginDaoImpl = new LoginDaoImpl();
		}
		
		return loginDaoImpl;
	}
	
	public String getUserRole(String username, String password) {
		
		String role = "";
		query = "SELECT user_role FROM user WHERE user_id='"+ username + "' AND user_password='" + password + "'";
		
		try {
			connector = Connector.getInstance();
			con = connector.getConnection();
			statement = con.createStatement();
			rs = statement.executeQuery(query);
			
			while(rs.next()) {
				role = rs.getString("user_role");
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try { if(rs != null) rs.close(); } catch(Exception e) {  e.printStackTrace(); }
			try { if(statement != null) statement.close(); } catch(Exception e) {  e.printStackTrace(); }
			try { if(con != null) con.close(); } catch(Exception e) {  e.printStackTrace(); }
		}
		
		return role;
	}

	public String getUserName(String username, String password) {
		String name = "";
		query = "SELECT user_name FROM user WHERE user_id='"+ username + "' AND user_password='" + password + "'";
		
		try {
			connector = Connector.getInstance();
			con = connector.getConnection();
			statement = con.createStatement();
			rs = statement.executeQuery(query);
			
			while(rs.next()) {
				name = rs.getString("user_name");
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			try { if(rs != null) rs.close(); } catch(Exception e) {  e.printStackTrace(); }
			try { if(statement != null) statement.close(); } catch(Exception e) {  e.printStackTrace(); }
			try { if(con != null) con.close(); } catch(Exception e) {  e.printStackTrace(); }
		}
		
		return name;
	}
	
}
